package ro.pub.cs.stadium.platform;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Shell;

import ro.pub.cs.stadium.platform.general.OkCancelDialog;
import sun.security.action.GetLongAction;

public class SettingsWindow extends OkCancelDialog {
	private final ScrolledComposite propContainer;
	private final List propList;
	
	private final Sash splitter;
	private FormData listData = null;

	
	public SettingsWindow(Shell mainWnd) {
		super(mainWnd, true);
		
		getWndShell().setText("StaDiUM Settings");
		
		
		// Options list
		
		propList = new List(getWndShell(), SWT.BORDER | SWT.V_SCROLL);
		
		
		listData = new FormData();
		listData.left = new FormAttachment(0);
		listData.top = new FormAttachment(0);
		listData.bottom = new FormAttachment(getButtonSep(), -10);
		listData.width = 200;
		propList.setLayoutData(listData);
		
		// Splitter
		
		splitter = new Sash(getWndShell(), SWT.VERTICAL);
		FormData splitData = new FormData();
		splitData.left = new FormAttachment(propList);
		splitData.top = new FormAttachment(0);
		splitData.bottom = new FormAttachment(getButtonSep(), -10);
		splitter.setLayoutData(splitData);
		
		
		// Properties container
		propContainer = new ScrolledComposite(getWndShell(), SWT.NONE);
		FormData contData = new FormData();
		contData.left = new FormAttachment(splitter);
		contData.right = new FormAttachment(100);
		contData.top = new FormAttachment(0);
		contData.bottom = new FormAttachment(getButtonSep(), -10);
		propContainer.setLayoutData(contData);	
		
		
		splitter.addSelectionListener(this);
	}
	
	@Override
	public int showModal() {
		return super.showModal();
	}

	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		super.widgetSelected(e);
		
		if (e.widget == splitter) {
			Rectangle splitRect = splitter.getBounds();
			Rectangle listRect = propList.getBounds();
			
			if (splitRect.x != e.x) {
				listData.width = listRect.width + (e.x - splitRect.x) - 20; 
				getWndShell().layout();
			}
		}
		
	}

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.platform.general.OkCancelDialog#doCancelAction()
	 */
	@Override
	protected void doCancelAction() {
		modalResult = -1;
		getWndShell().close();
		
	}

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.platform.general.OkCancelDialog#doOkAction()
	 */
	@Override
	protected void doOkAction() {
		modalResult = 0;
		getWndShell().close();
		
	}
	
	
}
